package org.mbds.android.sismapp.activity;

import org.mbds.android.location.GPSTools;
import org.mbds.android.sismapp.SismappMenu;
import org.mbds.android.sismapp.R;
import org.mbds.android.sismapp.SismappApplication;
import org.mbds.android.sismapp.SismappStaticCommons;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.google.android.gms.common.GooglePlayServicesUtil;

public class LoginActivity extends Activity {
	public static final String TAG = "Welcome";
	public static final String FIRST_NAME="firstname";
	public static final String LAST_NAME="lastname";

	public static String firstName;
	public static String lastName;
	private ViewSwitcher switcher;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SharedPreferences settings = getSharedPreferences(
				SismappApplication.PREFS, Context.MODE_PRIVATE);
		firstName = settings.getString(FIRST_NAME, null);
		lastName= settings.getString(LAST_NAME, null);  
		setContentView(R.layout.activity_login);
		switcher = (ViewSwitcher) findViewById(R.id.switcher_login);
		if (firstName!=null && lastName!=null) {
			if (firstName!=null)
				SismappStaticCommons.setFirstName(firstName);
			if (lastName!=null)
				SismappStaticCommons.setLastName(lastName);
			welcome(firstName, lastName);
		}
		int ok = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) ;
		if (ok!=0)
			Toast.makeText(this, "Google Play Services doit �tre install�", Toast.LENGTH_LONG).show();
	}
	
	public void update(View view) {
		((EditText) findViewById(R.id.txt_firstname)).setText(firstName);
		((EditText) findViewById(R.id.txt_lastname)).setText(lastName);
		switcher.showPrevious();
	}
	
	public void handleLogin(View view) {
		firstName = ((EditText) findViewById(R.id.txt_firstname)).getText().toString();
		lastName = ((EditText) findViewById(R.id.txt_lastname)).getText().toString();

		if (!TextUtils.isEmpty(firstName) && !TextUtils.isEmpty(lastName)) {
			SharedPreferences settings = getSharedPreferences(
					SismappApplication.PREFS, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = settings.edit();

			editor.putString(FIRST_NAME, firstName);
			editor.putString(LAST_NAME, lastName);


			editor.commit();
			welcome(firstName, lastName);
		}
	}
	
	public void welcome(String first, String last) {
		TextView txt_welcome = (TextView) findViewById(R.id.txt_welcome);
		txt_welcome.setText("Welcome "+first+" "+last+"\n"
		+SismappStaticCommons.getDeviceName()+"\n"
				+GPSTools.getAddress(GPSTools.getLastKnownPosition(), this));
		switcher.showNext();
	}

	/***********************/
	/* Create options menu */
	/***********************/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SismappMenu.onCreateOptionsMenu(this, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/************************/
	/* Prepare options menu */
	/************************/
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		SismappMenu.onPrepareOptionsMenu(this, menu);
		return super.onPrepareOptionsMenu(menu);
	}

	/*******************************/
	/* Manage menu item selection */
	/*******************************/
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		SismappMenu.onOptionsItemSelected(this, item);
		return super.onOptionsItemSelected(item);
	}

}
